package cn.anecansaitin.whimsy.common.listener;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.common.capabilities.battle.BattleMode;
import cn.anecansaitin.whimsy.common.capabilities.battle.IBattleMode;
import cn.anecansaitin.whimsy.common.network.BattleModeUpdatePack;
import cn.anecansaitin.whimsy.common.network.NetworkHelper;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.TickEvent;

@Mod.EventBusSubscriber(modid = Whimsy.MODID)
public class OnPlayerTick {
    @SubscribeEvent
    public static void tick(TickEvent.PlayerTickEvent event) {
        if (event.phase == TickEvent.Phase.START) {
            return;
        }

        Player player = event.player;

        IBattleMode iBattleMode = player.getCapability(IBattleMode.BATTLE_MODE);
        iBattleMode.tick(player);

        //检查玩家的战斗模式能力，如果有更新就发包
        if (player instanceof ServerPlayer serverPlayer && iBattleMode instanceof BattleMode battleMode && battleMode.isDirt()) {
            battleMode.setClean();
            NetworkHelper.toClientPlayer(serverPlayer, new BattleModeUpdatePack(battleMode));
        }
    }
}
